﻿using Cloud.Update.Core.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Cloud.Update.Core.Utils
{
    public class BackUpUtil
    {
        public delegate void BackUpProgressChangedHandler(BaseModel progress);
        public event BackUpProgressChangedHandler BackUpProgressChanged;
        public delegate void BackUpFinishHandler(bool isSuccess, UpdateState state, string msg = null);
        public event BackUpFinishHandler BackUpFinish;
        /// <summary>
        /// 要备份的文件路径
        /// </summary>
        private string sourceFolder;
        /// <summary>
        /// 备份到路径
        /// </summary>
        private string destFolder;
        /// <summary>
        /// 1 备份 2 还原
        /// </summary>
        private int type;
        public BackUpUtil(string sourceFolder, string destFolder,int type)
        {
            this.sourceFolder = sourceFolder;
            this.destFolder = destFolder;
            this.type = type;
            totalFiles = 0;
            currentFiles = 0;
        }
        private int totalFiles;
        private int currentFiles;

        private int rate;


        public void Backup() {
            if (Directory.Exists(sourceFolder) && Directory.Exists(destFolder))
            {
                CopyFolder(sourceFolder, destFolder);
            }
            if (type == 1)
                BackUpFinish?.Invoke(true, UpdateState.备份成功, "备份成功!");
            else if (type == 2) {
                BackUpFinish?.Invoke(true, UpdateState.还原成功, "成功还原至上一版本!");
            }

        }
        /// <summary>
        /// 复制文件夹及文件
        /// </summary>
        /// <param name="sourceFolder">原文件路径</param>
        /// <param name="destFolder">目标文件路径</param>
        /// <returns></returns>
        private int CopyFolder(string sourceFolder,string destFolder)
        {
            try
            {
                //如果目标路径不存在,则创建目标路径
                if (!Directory.Exists(destFolder))
                {
                    Directory.CreateDirectory(destFolder);
                }
                //得到原文件根目录下的所有文件
                string[] files = Directory.GetFiles(sourceFolder);
                totalFiles += files.Length;
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);
                    string dest = Path.Combine(destFolder, name);
                    File.Copy(file, dest, true);//复制文件

                    currentFiles++;

                    rate = (int)((float)currentFiles / (float)totalFiles * 100);

                    BackUpProgressChanged?.Invoke(new BaseModel { TotalSize = totalFiles, CurrentSize = currentFiles, ProgressRate = rate, State = type == 1? UpdateState.备份中: UpdateState.还原中 });
                }
                //得到原文件根目录下的所有文件夹
                string[] folders = Directory.GetDirectories(sourceFolder);
                foreach (string folder in folders)
                {
                    string name = Path.GetFileName(folder);
                    string dest = Path.Combine(destFolder, name);
                    CopyFolder(folder, dest);//构建目标路径,递归复制文件
                }
                return 1;
            }
            catch (Exception e)
            {
                BackUpFinish?.Invoke(false,type == 1 ? UpdateState.备份失败 : UpdateState.还原失败, e.Message);
                throw e;
            }
        }
    }
}
